package com.apobates.jforum.grief.aurl.convertor;

import com.apobates.jforum.grief.aurl.core.AUrlMeta;
import com.apobates.jforum.grief.aurl.core.AUrlMetaBuilder;
import com.apobates.jforum.grief.aurl.spring.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * AUrlMetaBuilder的转换器
 */
public class BuilderConvertor implements AUrlMetaConvertor<AUrlMetaBuilder> {
    @Override
    public AUrlMetaBuilder convert(AUrlMeta aUrlMeta) throws IllegalStateException {
        return this.createBuilder(new MapConvertor().convert(aUrlMeta));
    }

    protected AUrlMetaBuilder createBuilder(Map<String, String> metaMap){
        Map<String, Optional<String>> temp = CollectionUtils.buildSafeMap(metaMap, List.of("schema", "host", "userInfo", "port", "path", "query", "fragment"));
        return AUrlMetaBuilder.getInstance()
                .scheme(temp.get("schema"))
                .host(temp.get("host"))
                .userInfo(temp.get("userInfo"))
                .port(temp.get("port"))
                .path(temp.get("path"))
                .query(temp.get("query"))
                .fragment(temp.get("fragment"));
    }
}
